/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.autosalon.ui.customer.action;

import com.autosalon.DAO.DaoFactory;
import com.autosalon.model.Adresa;
import com.autosalon.model.Role;
import com.autosalon.model.Zakaznik;
import com.autosalon.tools.Constants;
import com.autosalon.ui.customer.Customer;
import com.autosalon.ui.customer.state.CustomerStateHolder;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Component;
import com.vaadin.ui.Form;
import com.vaadin.ui.Label;
import com.vaadin.ui.TextField;
import com.vaadin.ui.Window.Notification;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 *
 * @author Kubík
 */
public class SaveUpdateCustomerAction extends AbstractCustomerAction {

    private final Zakaznik zakaznik;

    public SaveUpdateCustomerAction(Customer customer, Zakaznik zakaznik) {
        super(customer);
        this.zakaznik = zakaznik;
    }

    @Override
    public void buttonClick(ClickEvent event) {
        zakaznik.setMeno(customer.getTexFirstName().getValue().toString());
        zakaznik.setPrimeni(customer.getTexLastName().getValue().toString());
        zakaznik.setMobil(customer.getTexMobil().getValue().toString());
        zakaznik.setEmail(customer.getTexEmail().getValue().toString());
        zakaznik.setPoznamka(customer.getTexNote().getValue().toString());
        zakaznik.setPassword(customer.getTexPassword().getValue().toString());
        String propRole = "customer";
        if (customer.getComRole().isSelected("Seller")) {
            propRole = "Seller";
            customer.getComRole().select("customer");
        }
        Role role = (Role) DaoFactory.getinstance().getRole().getOneByParam(Constants.DB_ROLE_NAME, propRole);
        zakaznik.setRole(role);

        List<Adresa> adress = new ArrayList<Adresa>();
        List<Form> forms = customer.getNewAdress();
        //forms.remove(0);
        for (Iterator<Form> formsIt = forms.iterator(); formsIt.hasNext();) {
            Form form = formsIt.next();
            Iterator<Component> componentIt = form.getLayout().getComponentIterator();
            Adresa adresa = new Adresa();
            componentIt.next();
            TextField stat = (TextField) componentIt.next();
            TextField mesto = (TextField) componentIt.next();
            TextField ulice = (TextField) componentIt.next();
            TextField cisloPopisne = (TextField) componentIt.next();
            TextField cisloOrientacni = (TextField) componentIt.next();
            TextField psc = (TextField) componentIt.next();
            adresa.setStat(stat.getValue().toString());
            adresa.setMesto(mesto.getValue().toString());
            adresa.setUlice(ulice.getValue().toString());
            adresa.setCp(Integer.parseInt(cisloPopisne.getValue().toString()));
            adresa.setCo(Integer.parseInt(cisloOrientacni.getValue().toString()));
            adresa.setPsc(psc.getValue().toString());
            adress.add(adresa);
        }
        zakaznik.setAdresy(adress);
        DaoFactory.getinstance().getZakaznik().update(zakaznik);



        customer.getWindow().showNotification("Změna údajů proběhla úspěšně", Notification.TYPE_WARNING_MESSAGE);
        int id;
        try {
            id = customer.getSwitcher().getMainWindow().getZakaznik().getRole().getId().intValue();
        } catch (NullPointerException e) {
            id = 1;
        }
        CustomerStateHolder.setState(CustomerStateHolder.SAVE_UPDATE_CUSTOMER_STATE, id, super.customer);
    }
}
